<?php

class Admin_BlogController extends ControllerAction_Abstract  {
	private $_categories = null;
	private $_categories_array = array();
	
	public function preDispatch(){
		parent::preDispatch();
		$this->_loginUser->setCategoryInfo();
		$this->_categories = $this->_loginUser->getCategoryInfo();
		$this->view->categories = $this->_categories;
		if(!empty($this->_categories)){
			foreach ($this->_categories as $category){
				$this->_categories_array[$category->id] = $category->name;
			}
		}
		$this->view->categories_array = $this->_categories_array;
	}	
	
	public function listAction(){		
		$category_id = $this->getParam ( 'c_id' );
		if (!empty ( $category_id )) {
			$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_WHERE_ARRAY]['category_id = ?'] = $category_id ;
			$this->view->category_id = $category_id;
		}
		$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_WHERE_ARRAY]['user_id = ?'] = $this->_loginUser->id ;
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_SELECT] = array('id','title','abstract_context','create_at',
																		  'user_id','level','can_remark','category_id','remarks_count','tags_count');
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_ORDERBY] = array(Config_Table::TABLE_BLOG .'.level DESC',
		                                                                    Config_Table::TABLE_BLOG .'.create_at DESC');
		
		$options[ActionHelper_ActionPageList::OPTIONS_MODULE_SELECT] = Module_Impl_Blog_Select::getInstance()->setOptions($_select_options);
		$options[ActionHelper_ActionPageList::OPTIONS_MODULE_OPRATION] = Module_Impl_Blog_Opration::getInstance();
		$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_PAGENUMBER] = 4;
		$options[ActionHelper_ActionPageList::OPTIONS_PAGEURL] = $this->_pageInfo->getCurrentUrl().'/c_id/'.$category_id;
		$this->_helper->actionPageList->setOptions ( $options );		
		$this->_helper->actionPageList->process ();			
	}	
	
	public function setAction(){
		$this->view->title = $this->_('blog.list.set');
		$id = $this->_pageInfo->getId ();
		$options[Module_Impl_Blog_Select::SQLARRAY_OPTION_SELECT] = array('id','create_at',
																		  'level','can_remark'
																		  ,'category_id');		
			
		$blog_select = Module_Impl_Blog_Select::getInstance()->setOptions($options);		
		$blog = $blog_select->getById($id);
		$this->view->blog = $blog;
		$this->view->id = $id;
		$this->view->category_id = $blog->category_id;
		if ($this->getRequest ()->isPost ()) {
			$blog_opration = Module_Impl_Blog_Opration::getInstance();
			$blog_opration->update($id,$this->getPosts());
			$this->view->message = "Set OK";
			$this->render('message');
		}else{
			$this->render();
		}
	}
	
	public function deleteAction(){
		$this->view->title = $this->_('blog.list.delete');
		$id = $this->_pageInfo->getId ();	
		$options[Module_Impl_Blog_Select::SQLARRAY_OPTION_SELECT] = array('id','title','abstract_context','create_at',
																		  'user_id','level','can_remark'
																		  ,'category_id');	
		$blog_select = Module_Impl_Blog_Select::getInstance()->setOptions($options);		
		$blog = $blog_select->getById($id);
		$this->view->blog = $blog;
		$this->view->id = $id;
		$this->view->category_id = $blog->category_id;
		if ($this->getRequest ()->isPost ()) {
			$blog_opration = Module_Impl_Blog_Opration::getInstance();
			$blog_opration->delete($id);
			$this->view->message = "Delete OK";
			$this->render('message');
		}else{
			$this->render();
		}
	}
	
	public function newAction(){		
		$this->_helper->actionPost->setOptions ( $this->_getOptions() );
		$this->_helper->actionPost->process ();		
	}
	
	public function editAction(){
		$id = $this->_pageInfo->getId ();		
		$this->_helper->actionPost->setOptions ( $this->_getOptions(ActionHelper_ActionListWithPost::ACTION_EDIT,$id) );
		$this->_helper->actionPost->process ();		
	}
	
	protected function _getOptions($actionType = ActionHelper_ActionListWithPost::ACTION_NEW,$id = null,$category_id = null){
		$form = new Form_Blog_New ( );
		if (! empty ( $id ) and $actionType == ActionHelper_ActionListWithPost::ACTION_EDIT) {
			$form->getElement ( 'submit' )->setLabel ( $this->_ ( 'edit' ) );
		} else {
			$form->getElement ( 'submit' )->setLabel ( $this->_ ( 'new' ) );
		}
		
		$elementCategoryid = $form->getElement ( 'category_id' );
		$elementCategoryid->setMultiOptions($this->_categories_array);
		if(!empty($category_id)){
			$elementCategoryid->setValue($category_id);
		}
		
		$elementUserid = $form->getElement ( 'user_id' );
		$elementUserid->setValue($this->_loginUser->id);
		
		if ($actionType == ActionHelper_ActionListWithPost::ACTION_EDIT) {
			$url = $this->_pageInfo->getCurrentUrl () . '/do/' . $actionType;
		} else {
			$url = $this->_pageInfo->getCurrentUrl () ;
		}
		if(!empty($category_id)){
			$url .= $url . '/c_id/' . $category_id ;
		}
		if(!empty($id)){
			$url .= '/id/' . $id;
		}
		$form->setAttrib ( 'action', $url );
		
		$options[ActionHelper_ActionPost::OPTIONS_FORM] = $form;
		$options[ActionHelper_ActionPost::OPTIONS_MODULE_SELECT] = Module_Impl_Blog_Select::getInstance();
		$options[ActionHelper_ActionPost::OPTIONS_MODULE_OPRATION] = Module_Impl_Blog_Opration::getInstance();
		if(!empty($id)){
			$options[ActionHelper_ActionPost::OPTIONS_INIT_ID] = $id;
		}
		return $options;
	}
	
	
}